import re
from l2ldjango.apps.usermanagement.models import *
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from django.forms import ModelForm
from django import forms
from l2ldjango.apps.lawschool.models import *

USER_CHOICES = (
    ('1', 'Attorney'),
    ('2', 'Student'),
    
)

class RegistrationFormedit(ModelForm):
	class Meta:
		model=User
		fields = ('first_name','last_name',) #,'username','email'

class UserProfiledit(ModelForm):
	class Meta:
		model=userprofile
        #fields=('secretquestion','answer','company')
		exclude=('user_status','user_type','licence','user','verified','last_contact_reg_period_start_year','last_contact_date') #,'profile_image''fb_id',
    
class RegistrationForm(ModelForm):
    class Meta:
        model=User
        fields = ('first_name','last_name','username','email',)
        
class UserProfileForm(ModelForm):
    class Meta:
        model=userprofile
        exclude=('verified','last_contact_reg_period_start_year','last_contact_date')        #'profile_image','fb_id',
            
class ShortRegistrationForm(forms.Form):
    full_name = forms.CharField(max_length=300,required=True)
    license_number = forms.IntegerField(required=True)
    email = forms.EmailField(required=True)
    password1 = forms.CharField(
                                label='Password',
                                widget=forms.PasswordInput()
                                )
    password2 = forms.CharField(
                                label='Password (Again)',
                                widget=forms.PasswordInput()
                                )
    
    def clean(self):
        cleaned_data = super(ShortRegistrationForm, self).clean()
        email = cleaned_data.get("email")
        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")
        existing_username=User.objects.filter(username=email)
        if password1 != password2:
            self._errors["password1"] = self.error_class(['Passwords do not match.'])
        if len(existing_username) > 0:
            self._errors["email"] = self.error_class(['Email address is already in-use! Please use another'])
        return cleaned_data
  
class RegistrationForm1(forms.Form):
  current_fname = forms.CharField(max_length=20,required=True,label="Current First Name")
  current_mname=forms.CharField(max_length=20,label="Current Middle Name")
  current_lname=forms.CharField(max_length=30,required=True,label="Current Last Name")  
  prior_fname=forms.CharField(max_length=20,required=False,label="Prior Middle Name")
  prior_mname=forms.CharField(max_length=20,required=False,label="Prior Middle Name")
  prior_lname=forms.CharField(max_length=30,required=False,label="Prior Last Name")
  email = forms.EmailField(label='Email')
  company_name=forms.CharField(max_length=100,required=False,label="Prior Last Name")
  username = forms.CharField(label='Username', max_length=30)
  password1 = forms.CharField(
     label='Password',
     widget=forms.PasswordInput()
  )
  password2 = forms.CharField(
     label='Password (Again)',
     widget=forms.PasswordInput()
  )
  telephone= forms.CharField(max_length=20,required=False,label="Telephone")
  
  
  address=forms.CharField(max_length=150,required=True,label="Address")
  room=forms.CharField(max_length=20,required=False,label="Room")
  floor=forms.CharField(max_length=20,required=False,label="Floor")
  address2=forms.CharField(max_length=100,required=False,label="Address2")
  address3=forms.CharField(max_length=100,required=False,label="Address3")
  zip=forms.IntegerField(required=True,label="Zip")
  zip4=forms.IntegerField(required="False",label="Zip4")
  telephone= forms.CharField(max_length=20,required=False,label="Telephone")
  judicial_department=forms.IntegerField(required=False,label="Judicial Department")
  admitted_year=forms.IntegerField(required=False,label="Admitted Year")
  #country=forms.ForeignKey(country)
  dob=forms.DateField(required=False,label="DOB")
  last_contact_date=forms.DateField(required=False,label="Last Contact Date")
  #user_status=forms.ForeignKey(status)
  last_contact_reg_period_start_year=forms.IntegerField(required=False,label="Last Contact Reg period Start Year")
  filler=forms.CharField(max_length=48,required=False,label="Filler")
  state=forms.CharField(max_length=2,required=False,label="State")
  city=forms.CharField(label='City', max_length=30,required=False)
  user_type=forms.ChoiceField(choices=USER_CHOICES)
  profile_image=models.ImageField(null=False,blank=False,upload_to='profileimages')
  licence=forms.CharField(required=False,label='Licence Number(For Attorney)', max_length=30)
  graduation_date=forms.DateField(label="Graduation Date(for Students)",required=False)
  visible_in_search=forms.BooleanField(required=False)
  fb_link = forms.CharField(max_length=100, label="My FB link")
  web_link = forms.CharField(max_length=100, label="My Website")
  blg_link = forms.CharField(max_length=100, label="My Blog link")
  lnkd_link = forms.CharField(max_length=100, label="My LinkedIn link")
  fvrt_blg_link = forms.CharField(max_length=100, label="My favorite blog link")
  mh_rating = forms.CharField(max_length=10)
  enjoying = forms.CharField(max_length=20)
  #added_later after client's email  
  
  

  def clean_password2(self):
    if 'password1' in self.cleaned_data:
      password1 = self.cleaned_data['password1']
      password2 = self.cleaned_data['password2']
      if password1 == password2:
        return password2
    raise forms.ValidationError('Passwords do not match.')
  def clean_username(self): 
    username = self.cleaned_data['username'] 
    if not re.search(r'^\w+$', username): 
		raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.') 
    try: 
		User.objects.get(username=username) 
    except ObjectDoesNotExist: 
		return username 
    raise forms.ValidationError('Username is already taken.') 
  def clean_email(self):
	email=self.cleaned_data['email']
	try: 
		User.objects.get(email=email) 
	except ObjectDoesNotExist: 
		return email 
	raise forms.ValidationError('Email is already taken.') 
	
